Register-Lib PowerShell.Looping -InitDependencies PowerShell.Sugar

filter Write-Item($Prefix, [ScriptBlock] $Process, $Suffix) {
    if($Process){
        $str = (& $Process $_)
    } else {
        $str = $_
    }
    write-host $prefix $str $suffix
    $_
}

function RecoverableLoop {
    param(
        [scriptblock] $Process, 
        $IdentifierSelector = "ID",
        [switch] $Start, 
        [switch]$MarkCompleteWhenDone=$True,
        [System.Collections.IList] $ItemList
    )
    begin {
        if(!$ItemList){
            $usingScriptVar = $true;
            if(!$script:CompleteItems){
                $script:CompleteItems = New-Object System.Collections.ArrayList
            }
            $ItemList = $script:CompleteItems
        }
        if($Start){
            Log-Verbose 'Initializing recoverable loop'
            $ItemList.Clear()
        }
    }
    process { $item = $_
        $id = $item.$IdentifierSelector
        if(!$id) { 
            $id = $_
        }
        if(!($ItemList -contains $id )){
            if ($Process) {
                & $Process $item
            } else {
                $item
            }
            Log-Verbose "Processed $item"
            if($MarkCompleteWhenDone){
                $ItemList.Add($id) | out-null
            }
        }
    }
    end {
        $ItemList.Clear()
        if($usingScriptVar){
            Remove-Variable CompleteItems -Scope Script
        }    
    }
    #Partial-Loop
    #get-mpsites | ? { !($CompleteItems -contains $_)  } | % { & $Process; $CompleteItems += $_ }
}

function Count-Items {
	param ( $Name = "TempCount" )
	$list =@($input)
	Set-Variable -Name $Name -Value $list.Count -Scope script
	$list	
	if (Get-Variable $Name -scope script -ErrorAction SilentlyContinue){
        Remove-Variable $Name -scope script
    }
}
set-alias ci Count-Items



function Get-CurrentLoopId {
    if($PipelineVariables){
        $PipelineVariables['LoopId']
    }
}

function LoopNested($Properties) {
    $first, $Properties = $Properties
    if($first) {
        $input | lwpc | % { $_ |> $First | lwpc -ActionLabel:$first | LoopNested $Properties }
    } else {
        $input
    }
}

function LoopWithIndex--Experimental($Process, [REF]$index, $startIndex=0){
    begin {
        $index.Value = 0;
    }
    process {
        if($index.Value -ge $startIndex){
            $_
        }
        $index.Value++ | Out-Null
        
    }
}

function Get-ChildItemRecursive {
    param(
        [Parameter(ValueFromPipeline=$true)]
        $LiteralPath,
        $Filter,
        $Include,
        $Path,
        $Depth=5,
        $CurDepth=0
    )
    process {
        if($CurDepth -lt $Depth) {
            $CurDepth++
            $LiteralPath | ls -Filter:$Filter -Include:$Include | lwpc | % { 
                $_
                Get-ChildItemRecursive  -Depth:$Depth -CurDepth:($CurDepth)  -LiteralPath:$_ -Filter:$Filter -Include:$Include
            }
        }
    }
}
Set-Alias lsr Get-ChildItemRecursive


function IgnoreEmpty {
    $shouldReturn = $false;
    $queue = @()
    $input | % {
        if($_){ 
            $shouldReturn = $true;
            $_
        }        
    } | % {
        if($shouldReturn){
            $queue
            $_
            $queue = @()
        } else {
            $queue += $_
        }
    }
}
<#

function TestIgnoreEmpty {
    @(1..5 | ? { $_ -gt 6} | IgnoreEmpty | % { $Fail = $True; "Result" }).Length -eq 0 -and (!$Fail)
    @($null | IgnoreEmpty | % { $Fail = $True; "Result" }).Length -eq 0 -and (!$Fail)
    @(1..3 | IgnoreEmpty | % { $Succeed = $True; "Result" }).Length -eq 3 -and $Succeed
    @(1 | IgnoreEmpty | % { $Succeed = $True; "Result" }).Length -eq 1 -and $Succeed    
}
TestIgnoreEmpty

#>


function Loop-WithVariable($Name){    
    begin {
    }
    process {
        $item = $_        
        Set-Variable -Name:$Name -Value:$item -scope 1        
        $item
    }
    end {
        Remove-Variable $Name
    }
}
New-Alias lwv Loop-WithVariable -ErrorAction SilentlyContinue